package cheqex.update;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.List;
import javax.swing.*;
import javax.swing.JSpinner.DateEditor;

import cheqex.update.DateSelector;

import cheqex.dao.CollectionCash;
import cheqex.dao.CollectionCheck;
import cheqex.dao.DepositCash;
import cheqex.dao.DepositCashPeer;
import cheqex.dao.DepositCheck;
import cheqex.dao.DepositCheckPeer;
import cheqex.dao.Withdrawals;
import cheqex.dao.WithdrawalsPeer;
import cheqex.update.controllers.DateController;
import cheqex.update.controllers.FilterSearchController;

import java.awt.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.lang.Object;

public class FilterSearchView extends JPanel implements ActionListener, ItemListener{
	private JComboBox filterMenu;
	private JComboBox searchMenu;
	
	private JButton filter;
	private JButton search;
	
	private JTextField filterField;
	private JTextField searchField;
	
	private JLabel label1;
	private JLabel label2;
	private JLabel label01;
	private JLabel label02;
	
	private DateSelector filterDateField;
	private DateSelector searchDateField;
	
	private JPanel filterSearchPanel;
	private JPanel jl;
	private JPanel jp;
	
	private CashInBankView cashinbankview;
	
	public JPanel getCahInBankView() {
		return cashinbankview;
	}
	
	public void setCashInBankView(CashInBankView cashinbankview) {
		this.cashinbankview = cashinbankview;
	}
	
	public FilterSearchView(CashInBankView cashinbankview){
		this.cashinbankview = cashinbankview;
		initialize(cashinbankview);
	}
	
	public void initialize(CashInBankView cashinbankview){
		this.filterSearchPanel = new JPanel(new GridLayout(2,0,0,0));
		
		String[] filterList = {"--FILTER--",
							   "Filter Deposit Cash by OR No.", 
							   "Filter Deposit Cash by OR Date",
							   "Filter Deposit Checks by PR No.",
							   "Filter Deposit Checks by PR Date",
							   "Filter Withdrawals by Check No.",
							   "Filter Withdrawals by Check Date"};
		
		String[] searchList = {"--SEARCH--",
							   "Search for a particular by OR No. and OR Date",
							   "Search for a particular by PR No. and PR Date",
							   "Search for a particular by Check No. and Check Date"};
		
		this.filterMenu = new JComboBox(filterList);
		filterMenu.addActionListener(this);
		filterMenu.addItemListener(this);
		
		this.searchMenu = new JComboBox(searchList);
		searchMenu.addActionListener(this);
		
		this.filter = new JButton("Filter");
		this.filter.setVisible(true);
		this.filter.setEnabled(false);
		filter.setMnemonic(KeyEvent.VK_D);
	    filter.setActionCommand("filter");
	    filter.addActionListener(this);
	    
		this.search = new JButton("Search");
		this.search.setVisible(true);
		this.search.setEnabled(false);
		search.setMnemonic(KeyEvent.VK_D);
	    search.setActionCommand("search");
	    search.addActionListener(this);
	    
		this.filterField = new JTextField(20);
		this.filterField.setVisible(false);
		this.filterField.addKeyListener(new KeyListener(){
			
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					filter();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		this.filterDateField = new DateSelector();
		this.filterDateField.setVisible(false);
		
		this.searchField = new JTextField(20);
		this.searchField.setVisible(false);
		this.searchField.addKeyListener(new KeyListener(){
			
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					search();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		this.searchDateField = new DateSelector();
		this.searchDateField.setVisible(false);
		
	    this.jp = new JPanel();
	    this.jp.add(filterField);
	    this.jp.add(filterDateField);
	    this.jp.add(filter);
	    this.jp.setVisible(true);
	    this.jp.setLayout(new GridLayout(3,2));
	       
	    this.jl = new JPanel();
	    this.jl.add(searchField);
	    this.jl.add(searchDateField);
	    this.jl.add(search);
	    this.jl.setVisible(true);
	    this.jl.setLayout(new GridLayout(4,2));
	    
	    this.add(filterMenu);
	    this.add(jp);
	    this.add(searchMenu);
	    this.add(jl);
	    this.setPreferredSize(new Dimension(50,100));
	    this.setVisible(true);
	    this.setLayout(new GridLayout(2,1));
	    
	}

	public String[][] filterDepositCashByORNo() {
		String nullField = new String(filterField.getText());
		
		if(nullField.length() == 0){
			JOptionPane.showMessageDialog(null, "Please enter the OR number.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		else if(FilterSearchController.isNumeric(filterField.getText()) == false){
			JOptionPane.showMessageDialog(null, "OR number must be an integer.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		else
		{
			List <DepositCash> dCashList;
			dCashList = FilterSearchController.filterDepositCashByORNo(filterField.getText());
			if (!dCashList.isEmpty()) {
				String[][] data = new String[dCashList.size()][3];
				for (int i=0;i<dCashList.size();i++) {
					DepositCash dCash = dCashList.get(i);
					data[i][0] = "" + dCash.getORNumber();
					data[i][1] = "" + dCash.getAmount();
					data[i][2] = "" + dCash.getParticulars();
					//System.out.println(dCash.toString());
				}
				return data;
			}
			else
			{
				JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
				return null;
			}
		}
	}
	
	
	public String[][] filterDepositCashByORDate() {
		List <DepositCash> dCashList;
		dCashList = FilterSearchController.filterDepositCashByORDate(filterDateField.getSelectedDate());
		if (!dCashList.isEmpty()) {
			String[][] data = new String[dCashList.size()][3];
			for (int i=0;i<dCashList.size();i++) {
				DepositCash dCash = dCashList.get(i);
				data[i][0] = "" + dCash.getORNumber();
				data[i][1] = "" + dCash.getAmount();
				data[i][2] = "" + dCash.getParticulars();
				//System.out.println(dCash.toString());
			}
			return data;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
	}
	
	public String[][] filterDepositCheckByPRNo() {
		String nullField = new String(filterField.getText());
		
		if(nullField.length() == 0){
			JOptionPane.showMessageDialog(null, "Please enter the PR number.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		else if(FilterSearchController.isNumeric(filterField.getText()) == false){
			JOptionPane.showMessageDialog(null, "PR number must be an integer.", "Halt!", JOptionPane.DEFAULT_OPTION);			
			return null;
		}	
		else 
		{
			List <DepositCheck> dCheckList;
			dCheckList = FilterSearchController.filterDepositCheckByPRNo(filterField.getText());
			if (!dCheckList.isEmpty()) {
				String[][] data = new String[dCheckList.size()][3];
				for (int i=0;i<dCheckList.size();i++) {
					DepositCheck dCheck = dCheckList.get(i);
					data[i][0] = "" + dCheck.getPRNumber();
					data[i][1] = "" + dCheck.getAmount();
					data[i][2] = "" + dCheck.getParticulars();
					//System.out.println(dCheck.toString());
				}
				return data;
			}
			else
			{
				JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
				return null;
			}	
		}
	}
	
	public String[][] filterDepositCheckByPRDate() {
		List <DepositCheck> dCheckList;
		dCheckList = FilterSearchController.filterDepositCheckByPRDate(filterDateField.getSelectedDate());
		if (!dCheckList.isEmpty()) {
			String[][] data = new String[dCheckList.size()][3];
			for (int i=0;i<dCheckList.size();i++) {
				DepositCheck dCheck = dCheckList.get(i);
				data[i][0] = "" + dCheck.getPRNumber();
				data[i][1] = "" + dCheck.getAmount();
				data[i][2] = "" + dCheck.getParticulars();
			}
			return data;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
	}
	
	public String[][] filterWithdrawalsByCheckNo() {
		String nullField = new String(filterField.getText());
		
		if(nullField.length() == 0){
			JOptionPane.showMessageDialog(null, "Please enter the check number.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		else if(FilterSearchController.isNumeric(filterField.getText()) == false){
			JOptionPane.showMessageDialog(null, "Check number must be an integer.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		else {
			List <Withdrawals> withdrawalList;
			withdrawalList = FilterSearchController.filterWithdrawalsByCheckNo(filterField.getText());
			if (!withdrawalList.isEmpty()) {
				String[][] data = new String[withdrawalList.size()][3];
				for (int i=0;i<withdrawalList.size();i++) {
					Withdrawals withdrawal = withdrawalList.get(i);
					data[i][0] = "" + withdrawal.getCheckNumber();
					data[i][1] = "" + withdrawal.getAmount();
					data[i][2] = "" + withdrawal.getParticulars();
				}
				return data;
			}
			else
			{ 
				JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
				return null;
			}
		}
	}
	
	public String[][] filterWithdrawalsByCheckDate() {
		List <Withdrawals> withdrawalList;
		withdrawalList = FilterSearchController.filterWithdrawalsByCheckDate(filterDateField.getSelectedDate());
		if (withdrawalList!=null) {
			String[][] data = new String[withdrawalList.size()][3];
			for (int i=0;i<withdrawalList.size();i++) {
				Withdrawals withdrawal = withdrawalList.get(i);
				data[i][0] = "" + withdrawal.getCheckNumber();
				data[i][1] = "" + withdrawal.getAmount();
				data[i][2] = "" + withdrawal.getParticulars();
			}
			return data;
		}
		else
		{ 
			JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
	}
	
	public String[][] searchDepositCashParticular() {
		String nullField = new String(searchField.getText());
		
		if(nullField.length() == 0){
			JOptionPane.showMessageDialog(null, "Please enter the OR number.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		
		else if(FilterSearchController.isNumeric(searchField.getText()) == false){
			JOptionPane.showMessageDialog(null, "OR number must be an integer.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		
		else{
			List <DepositCash> dCashList;
			dCashList = FilterSearchController.searchDepositCashParticular(searchField.getText(), searchDateField.getSelectedDate());
			if (!dCashList.isEmpty()) {
				String data[][] = new String[dCashList.size()][4];
				for (int i=0;i<dCashList.size();i++) {
					DepositCash dCash = dCashList.get(i);
					data[i][0] = "Deposit";
					data[i][1] = "" + dCash.getORNumber();
					data[i][2] = "" + dCash.getAmount();
					data[i][3] = "" + dCash.getParticulars();
				}
				return data;
			}
			else
			{
				
				return null;
			}
		}
	}
	
	public String[][] searchDepositCheckParticular() {
		String nullField = new String(searchField.getText());
		
		if(nullField.length() == 0){
			JOptionPane.showMessageDialog(null, "Please enter the PR number.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		
		else if(FilterSearchController.isNumeric(searchField.getText()) == false){
			JOptionPane.showMessageDialog(null, "PR number must be an integer.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		
		else {
			List <DepositCheck> dCheckList;
			dCheckList = FilterSearchController.searchDepositCheckParticular(searchField.getText(), searchDateField.getSelectedDate());
			if (!dCheckList.isEmpty()) {
				String[][] data = new String[dCheckList.size()][4];
				for (int i=0;i<dCheckList.size();i++) {
					DepositCheck dCheck = dCheckList.get(i);
					data[i][0] = "Deposit";
					data[i][1] = "" + dCheck.getPRNumber();
					data[i][2] = "" + dCheck.getAmount();
					data[i][3] = "" + dCheck.getParticulars();
				}
				return data;
			}
			else
			{
				
				return null;
			}
		}
	}

	public String[][] searchCollectionCashParticular() {
		List <CollectionCash> cCashList;
		cCashList = FilterSearchController.searchCollectionCashParticular(searchField.getText(), searchDateField.getSelectedDate());
		if (!cCashList.isEmpty()) {
			String[][] data = new String[cCashList.size()][4];
			for (int i=0;i<cCashList.size();i++) {
				CollectionCash cCash = cCashList.get(i);
				data[i][0] = "Collections";
				data[i][1] = "" + cCash.getORNumber();
				data[i][2] = "" + cCash.getAmount();
				data[i][3] = "" + cCash.getParticulars();
			}
			return data;
		}
		else
		{
			
			return null;
		}
	}
	
	public String[][] searchCollectionCheckParticular() {
		List <CollectionCheck> cCheckList;
		cCheckList = FilterSearchController.searchCollectionCheckParticular(searchField.getText(), searchDateField.getSelectedDate());
		if (!cCheckList.isEmpty()) {
			String[][] data = new String[cCheckList.size()][4];
			for (int i=0;i<cCheckList.size();i++) {
				CollectionCheck cCheck = cCheckList.get(i);
				data[i][0] = "Collections";
				data[i][1] = "" + cCheck.getPRNumber();
				data[i][2] = "" + cCheck.getAmount();
				data[i][3] = "" + cCheck.getParticulars();
			}
			return data;
		}
		
		else
		{
			return null;
		}
	}
	
	public String[][] searchWithdrawalsParticular() {
		String nullField = new String(searchField.getText());
		
		
		 if(nullField.length() == 0){
			JOptionPane.showMessageDialog(null, "Please enter the check number.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		 
		 else if(FilterSearchController.isNumeric(searchField.getText()) == false){
			 JOptionPane.showMessageDialog(null, "Check number must be an integer.", "Halt!", JOptionPane.DEFAULT_OPTION);
			return null;
		}
		
		else {
			List <Withdrawals> withdrawalList;
			withdrawalList = FilterSearchController.searchWithdrawalsParticular(searchField.getText(), searchDateField.getSelectedDate());
			if (!withdrawalList.isEmpty()) {
				String[][] data = new String[withdrawalList.size()][3];
				for (int i=0;i<withdrawalList.size();i++) {
					Withdrawals withdrawal = withdrawalList.get(i);
					data[i][0] = "" + withdrawal.getCheckNumber();
					data[i][1] = "" + withdrawal.getAmount();
					data[i][2] = "" + withdrawal.getParticulars();
				}
				return data;
			}
			else
			{	
				JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
				return null;
			}	
		}
	}
	
	private void buttonEnable(){
		if(filterMenu.getSelectedIndex() == 0){
			this.filter.setEnabled(false);
		}
		
		else this.filter.setEnabled(true);
		
		if(searchMenu.getSelectedIndex() == 0){
			this.search.setEnabled(false);
		}
		
		else this.search.setEnabled(true);
	}
	
	private void fieldVisible(){
		if(filterMenu.getSelectedIndex() == 0){
			this.filterField.setVisible(false);
		}
		
		else if(filterMenu.getSelectedIndex() == 1 || filterMenu.getSelectedIndex() == 3 || filterMenu.getSelectedIndex() == 5){
			this.filterField.setVisible(true);
			this.filterDateField.setVisible(false);
		}
		
		if(searchMenu.getSelectedIndex() == 0){
			this.searchField.setVisible(false);
		}
		
		else this.searchField.setVisible(true);
	}
	
	private void dateVisible(){
		if(filterMenu.getSelectedIndex() == 0){
			this.filterDateField.setVisible(false);
		}
		
		else if(filterMenu.getSelectedIndex() == 2 || filterMenu.getSelectedIndex() == 4 || filterMenu.getSelectedIndex() == 6){
			this.filterDateField.setVisible(true);
			this.filterField.setVisible(false);
		}
		
		if(searchMenu.getSelectedIndex() == 0){
			this.searchDateField.setVisible(false);
		}
		
		else this.searchDateField.setVisible(true);
	}
	
	private String[] cashColumns(){
		return new String[]
		      			{"OR Number", "Amount", "Particulars"};
	}
	
	private String[] checkColumns(){
		return new String[]
		      			{"PR Number", "Amount", "Particulars"};
	}
	
	private String[] withdrawalsColumns(){
		return new String[]
		      			{"Check Number", "Amount", "Particulars"};
	}
	
	private String[] ORColumns(){
		return new String[]
		      			{"Table","OR Number", "Amount", "Particulars"};
	}
	
	private String[] PRColumns(){
		return new String[]
		      			{"Table","PR Number", "Amount", "Particulars"};
	}
	
	private void filter(){
		if(filterMenu.getSelectedIndex() != 0){
			String input = new String(filterField.getText());
			switch (filterMenu.getSelectedIndex()) {
			case 1 :
					if(filterDepositCashByORNo()!=null){  
						this.cashinbankview.populateTable(filterDepositCashByORNo(), cashColumns() );
					}//else JOptionPane.showMessageDialog(null,"An error happened.", "Error", JOptionPane.WARNING_MESSAGE); 
					break;
			case 2 : 
					if(filterDepositCashByORDate()!=null){
						this.cashinbankview.populateTable(filterDepositCashByORDate(), cashColumns());
					}//else JOptionPane.showMessageDialog(null,"An error happened.", "Error", JOptionPane.WARNING_MESSAGE);
					break;
			case 3 : 
					if(filterDepositCheckByPRNo()!=null)
						this.cashinbankview.populateTable(filterDepositCheckByPRNo(), checkColumns());
					break;
			case 4 : 
					if(filterDepositCheckByPRDate()!=null)
						this.cashinbankview.populateTable(filterDepositCheckByPRDate(), checkColumns());
					break;
			case 5 : 
					if(filterWithdrawalsByCheckNo()!=null)
						this.cashinbankview.populateTable(filterWithdrawalsByCheckNo(), withdrawalsColumns());  
					break;
			case 6 :
					if(filterWithdrawalsByCheckDate()!=null)
						this.cashinbankview.populateTable(filterWithdrawalsByCheckDate(), withdrawalsColumns());
					break;
			}
		}

	}
	
	private void search(){
		if(searchMenu.getSelectedIndex() != 0){
			String input = new String(searchField.getText());
			int a, b=0;
			switch (searchMenu.getSelectedIndex()) {
			case 1 : 
					if(searchDepositCashParticular()!=null || searchCollectionCashParticular()!=null){
						
						if(searchDepositCashParticular()!=null) 
							a = searchDepositCashParticular().length;
						else a = 0;
						
						if(searchCollectionCashParticular()!=null)
							b = searchCollectionCashParticular().length;
						else b=0;
						
						int c = a+b;
						int d = 0;
						String data[][] = new String[c][4];
						for (int i=0;i<c;i++) {
							if(i<a){
								data[i][0] = ""+searchDepositCashParticular()[i][0];
								data[i][1] = ""+searchDepositCashParticular()[i][1];
								data[i][2] = ""+searchDepositCashParticular()[i][2];
								data[i][3] = ""+searchDepositCashParticular()[i][3];
							}
							else
							{
								data[i][0] = ""+searchCollectionCashParticular()[d][0];
								data[i][1] = ""+searchCollectionCashParticular()[d][1];
								data[i][2] = ""+searchCollectionCashParticular()[d][2];
								data[i][3] = ""+searchCollectionCashParticular()[d][3];
								d++;
							}
							this.cashinbankview.populateTable(data, ORColumns());
						}
					}else JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);
					 break;
			case 2 :
					if(searchDepositCheckParticular()!=null || searchCollectionCheckParticular()!=null){
						
						if(searchDepositCheckParticular()!=null) 
							a = searchDepositCheckParticular().length;
						else a = 0;
						
						if(searchCollectionCheckParticular()!=null)
							b = searchCollectionCheckParticular().length;
						else b=0;
						
						int c = a + b;
						int d = 0;
						String data[][] = new String[c][4];
						for (int i=0;i<c;i++) {
							if(i<a){
								data[i][0] = ""+searchDepositCheckParticular()[i][0];
								data[i][1] = ""+searchDepositCheckParticular()[i][1];
								data[i][2] = ""+searchDepositCheckParticular()[i][2];
								data[i][3] = ""+searchDepositCheckParticular()[i][3];
							}
							else
							{
								data[i][0] = ""+searchCollectionCheckParticular()[d][0];
								data[i][1] = ""+searchCollectionCheckParticular()[d][1];
								data[i][2] = ""+searchCollectionCheckParticular()[d][2];
								data[i][3] = ""+searchCollectionCheckParticular()[d][3];
								d++;
							}
							this.cashinbankview.populateTable(data, PRColumns());
						}	
					}else 
						JOptionPane.showMessageDialog(null, "No data found.", "Halt!", JOptionPane.DEFAULT_OPTION);	
					 break;
			case 3 : 
					if(searchWithdrawalsParticular()!=null)
						this.cashinbankview.populateTable(searchWithdrawalsParticular(), withdrawalsColumns());
					break;
			}
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		buttonEnable();
		fieldVisible();
		dateVisible();
		if ("filter".equals(e.getActionCommand())) {
				filter();
		}

		if ("search".equals(e.getActionCommand())) {
			search();
		}
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	
}